$(function() {
	let page;
	/**
	 * 加载分页数据
	 * @param {int} current
	 */
	let loadData = function(current=1){
		$.get(
			"report/report-list.do",
			"current="+current,
			function(data){
				formatData(data.list);
				formatPage(data);
			},"json"
		);	
	}
	let datagrid = $("#datagrid");
	let template  = datagrid.children().remove();
	/**
	 * 格式化数据
	 * @param {Object} list
	 */
	let formatData = function(list){
		datagrid.empty();
		$.each(list,function(i,item){
			let tr = template.clone().appendTo(datagrid);
			let reportRealName;
			item.rowNumber=i+1;
			$.each(item, function(name,value) {
				tr.find("._"+name).text(value);
			});
			$.post(
				"user/get-realName.do",
				"userName="+item.reportUserName,
				function(data){
					fn(data);
				},"json"
			);
			let fn=function(data){
				tr.find("._reportRealName").text(data.userRealName);
			}
			$.post(
				"newsComments/comment-realName.do",
				"commentId="+item.reportCommentId,
				function(data){
					fn1(data);
				},"json"
			);
			let fn1=function(data){
				tr.find("._commentRealName").text(data.userRealName);
			}
			$.post(
				"newsComments/news-title.do",
				"commentId="+item.reportCommentId+"&falg=true",
				function(data){
					fn2(data);
				},"json"
			);
			$.get(
				"newsComments/news-title.do",
				"commentId="+item.reportCommentId+"&falg=false",
				function(data){
					fn3(data);
				},"json"
			);
			let fn3=function(data){
				tr.find("._reportCommentContent").text(data.commentContent);
			}
			let fn2=function(data){
				tr.find("._commentNewsTitle").text(data.newsTitle);
				tr.find("._commentNewsTitle").attr("href","news-show-"+data.newsId+".html")
			}
			tr.find("._reportHandles").attr("data-id",item.reportId);
		});
	}
	
	
	let pagination = $(".pagination");
	let liTemplate = pagination.children(":eq(1)").remove();
	/**
	 * 格式化页码
	 * @param {Object} p
	 */
	let formatPage = function(p){
		pagination.children(":not(:eq(0),:eq(-1))").remove();
		
		pagination.children(":last").attr("data-page",p.pages);
		
	
		
		$.each(p.navigatepageNums,function(index,i){
			let li = liTemplate.clone().insertBefore(pagination.children(":eq(-1)"));
			li.children("a").text(i);
			li.attr("data-page",i);
			if(p.pageNum==i){
				li.addClass("active");
			}
		});

		
		
	}

	/**
	 * 处理分页事件
	 */
	pagination.on("click","[data-page]",function(){
		page = $(this).data("page");
		loadData(page);
	});

	loadData();
	
	$("body").on("change", "._reportHandles", function() {
		let option=$(this).val();
		let handles;
		
		if(option==1){
			handles=true;
		}
		if(option==2){
			handles=false;
		}
		console.info(handles);
		$.post(
			"report/report-handle.do",
			{"reportId": $(this).data("id"),"handle":handles},
			function(data) {
				if(data.ok) {
					toastr.success("处理成功");
					loadData(page);
				} else {
					toastr.error(data.message);
				}
			}, "json"
		);
		return false;
	});
});
